package com.caine.multithread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: CaineZhu
 * @Description:
 * @Date: Created in 13:43 2020/11/4
 * @Modified By:
 */
public class Demo02 {
    AtomicInteger count = new AtomicInteger();
    Object obj = new Object();
    
    public int add(){
        return count.addAndGet(1);
    }
    
    public static void main(String[] args) {
        Demo02 demo02 = new Demo02();
        new Thread(()->{
            synchronized (demo02.obj) {
                try {
                    demo02.obj.wait();
                    System.out.println(Thread.currentThread() + "exit");
                    demo02.obj.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(()->{
            synchronized (demo02.obj) {
                int add = 0;
                while (add < 9) {
                    add = demo02.add();
                    System.out.println("add = " + add);
                    if (add == 4) {
                        try {
                            demo02.obj.notify();
                            demo02.obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }).start();
    }
}
